package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqTenant;
import cloud.maque.admin.mapper.MqTenantMapper;
import cloud.maque.admin.service.MqTenantService;
import cloud.maque.framework.common.exception.BusinessException;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author 4color
 * @description 租户
 * @date 2023-03-17
 */
@Service
public class MqTenantServiceImpl extends ServiceImpl<MqTenantMapper, MqTenant> implements MqTenantService {

    @Override
    public boolean logicDeleteById(String id) {
        return this.removeById(id);
    }

    @Override
    public MqTenant saveTenant(MqTenant mqTenant) {

        Assert.hasText(mqTenant.getTenantName(), "租户名称不能为空");
        mqTenant.setTenantCode(mqTenant.getTenantCode().trim());
        Assert.hasText(mqTenant.getTenantCode(), "租户代码不能为空");

        if (StrUtil.isEmptyIfStr(mqTenant.getTenantId()) || this.getById(mqTenant.getTenantId()) == null) {
            if (mqTenant.getTenantCode().equals("maque") || mqTenant.getTenantCode().equals("麻雀")) {
                throw new BusinessException("租户代码不允许注册");
            }
        }

        MqTenant mqTenant1 = getByCode(mqTenant.getTenantCode());
        if (mqTenant1 != null && !(mqTenant1.getTenantId().equals(mqTenant.getTenantId()))) {
            throw new BusinessException("租户代码已存在");
        }

        MqTenant mqTenant2 = getByName(mqTenant.getTenantName());
        if (mqTenant2 != null && !(mqTenant2.getTenantId().equals(mqTenant.getTenantId()))) {
            throw new BusinessException("租户名称已存在");
        }

        saveOrUpdate(mqTenant);
        return mqTenant;
    }

    @Override
    public MqTenant getByCode(String tenantCode) {
        QueryWrapper<MqTenant> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqTenant::getTenantCode, tenantCode);
        return this.getOne(queryWrapper, false);
    }

    public MqTenant getByName(String tenantName) {
        QueryWrapper<MqTenant> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqTenant::getTenantCode, tenantName);
        queryWrapper.lambda().eq(MqTenant::getDisabled, 0);
        return this.getOne(queryWrapper, false);
    }

    @Override
    public PageInfo<MqTenant> page(PaginateDataResquest<MqTenant> pageReq) {

        PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize());
        QueryWrapper<MqTenant> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantName())) {
            wrapper.lambda().like(MqTenant::getTenantName, pageReq.getData().getTenantName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantCode())) {
            wrapper.lambda().like(MqTenant::getTenantCode, pageReq.getData().getTenantCode());
        }
        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqTenant::getTenantId, pageReq.getData().getTenantId());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getComboId())) {
            wrapper.lambda().eq(MqTenant::getComboId, pageReq.getData().getComboId());
        }

        wrapper.lambda().orderByDesc(MqTenant::getCreateAt);
        wrapper.lambda().orderByAsc(MqTenant::getTenantId);
        List<MqTenant> lists = this.baseMapper.selectList(wrapper);
        PageInfo<MqTenant> pageInfo = new PageInfo<>(lists);

        return pageInfo;
    }

    @Override
    public Integer getCountByComboId(String comboId) {
        QueryWrapper<MqTenant> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqTenant::getComboId, comboId);
        return this.baseMapper.selectCount(wrapper);
    }
}
